package com.sunseaypw.web.serverlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns="/hi")
public class HiServerlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
            throws ServletException, java.io.IOException {
        System.out.println(req.getHeader("User-Agent"));
        req.setAttribute("User-Agent", "aaaaaabbbbb");    
        System.out.println(req.getAttribute("User-Agent").toString());
        resp.sendRedirect("/hello");//302 重定向 浏览器端每次遇到老的/hi 都会再次向服务器发送老的url /hi 再由服务器端根据实际情况进行响应
        /* 301永久重定向，浏览器端将会标记老的/hi 路径到 新的/hello路径的匹配，每次遇到老的/hi就直接发送新的/hellourl 不再向服务器发送老的/hi */
        // resp.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); // 301
        // resp.setHeader("Location", "/hello");
    }
}
